import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_play_android/ui/main_screen.dart';

class SplashScreen extends StatefulWidget {
  const SplashScreen({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  var cutDown = 5;
  late Timer _timer;

  @override
  void initState() {
    super.initState();
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      setState(() {
        cutDown--;
        if (cutDown <= 0) {
          Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(builder: (context) => const MainScreen()),
              (route) => false);
          _timer.cancel();
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Stack(
        alignment: AlignmentDirectional.topEnd,
        children: [
          Positioned.fill(
              child: Container(
                  color: Theme.of(context).primaryColor,
                  child: Image.asset(
                    "assets/images/splash.jpg",
                  ))),
          Positioned(
            child: TextButton(
              style: ButtonStyle(
                textStyle: MaterialStateProperty.all(
                    const TextStyle(fontSize: 15, color: Colors.black54)),
                backgroundColor: MaterialStateColor.resolveWith((states) =>
                    states.contains(MaterialState.pressed)
                        ? Colors.white38
                        : Colors.white),
                overlayColor: MaterialStateProperty.all(Colors.white70),
                padding: MaterialStateProperty.all(
                    const EdgeInsets.fromLTRB(10, 5, 10, 5)),
              ),
              onPressed: () {
                _timer.cancel();
                Navigator.pushAndRemoveUntil(
                    context,
                    MaterialPageRoute(builder: (context) => const MainScreen()),
                    (route) => false);
              },
              child: Text(
                "跳过 $cutDown",
              ),
            ),
            right: 15,
            top: 40,
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }
}
